//
//  FlyCollectionIndexView.m
//  Test
//
//  Created by lifayu on 2017/9/7.
//  Copyright © 2017年 贵阳星力荐外电子商务有限公司. All rights reserved.
//

#import "FlyCollectionIndexView.h"

@implementation FlyCollectionIndexView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        _indexFont = [UIFont systemFontOfSize:14.0];
        _indexColor = [UIColor blueColor];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    if(self.indexLabels && self.indexLabels.count > 0)
    {
        for (NSInteger index = 0; index < self.indexLabels.count; index ++)
        {
            UILabel *label = self.indexLabels[index];
            label.frame = CGRectMake(0, FlyCollectionViewIndexHeight*index, self.bounds.size.width, FlyCollectionViewIndexHeight);
        }
    }
}

- (NSMutableArray *)indexLabels
{
    if(!_indexLabels)
    {
        _indexLabels = [NSMutableArray array];
    }
    return _indexLabels;
}

- (void)setIndexFont:(UIFont *)indexFont
{
    _indexFont = indexFont;
    [self setIndexFont:_indexFont color:_indexColor];
}

- (void)setIndexColor:(UIColor *)indexColor
{
    _indexColor = indexColor;
    [self setIndexFont:_indexFont color:_indexColor];
}

- (void)setIndexFont:(UIFont *)font color:(UIColor *)color
{
    _indexFont = font;
    _indexColor = color;
    for (UILabel *label in self.indexLabels)
    {
        label.textColor = _indexColor;
        label.font = _indexFont;
    }
}


- (void)setIndexs:(NSArray *)indexs
{
    if(_indexs != indexs)
    {
        _indexs = indexs;
        [self _setupIndexLabels];
    }
}

- (void)_setupIndexLabels
{
    if(self.indexLabels && self.indexLabels.count > 0)
    {
        for (UILabel *label in self.indexLabels)
        {
            [label removeFromSuperview];
        }
        [self.indexLabels removeAllObjects];
    }
    for (NSString *index in _indexs)
    {
        UILabel *label = [self indexLabelWithIndex:index];
        [self addSubview:label];
        [self.indexLabels addObject:label];
    }
}

- (UILabel *)indexLabelWithIndex:(NSString *)index
{
    UILabel *label = [UILabel new];
    label.text = index;
    label.font = _indexFont;
    label.textColor = _indexColor;
    label.textAlignment = NSTextAlignmentCenter;
    return label;
}

#pragma mark- response事件
//开始触摸
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self sendEventToDelegate:event];
    
    if(self.indexDelegate && [self.indexDelegate respondsToSelector:@selector(collectionIndexViewTouchesBegan:)])
    {
        [self.indexDelegate collectionIndexViewTouchesBegan:self];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
    [self sendEventToDelegate:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(self.indexDelegate && [self.indexDelegate respondsToSelector:@selector(collectionIndexViewTouchesEnd:)])
    {
        [self.indexDelegate collectionIndexViewTouchesEnd:self];
    }
}

//根据触摸事件的触摸点来算出点击的是第几个section
- (void)sendEventToDelegate:(UIEvent*)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint point = [touch locationInView:self];
    NSInteger index = ((NSInteger) floorf(point.y) / FlyCollectionViewIndexHeight);
    
    if (index< 0 || index > self.indexs.count - 1)
    {
        return;
    }
    if(self.indexDelegate && [self.indexDelegate respondsToSelector:@selector(collectionIndexView:didselectionAtIndex:title:)])
    {
        [self.indexDelegate collectionIndexView:self didselectionAtIndex:index title:self.indexs[index]];
    }
}

@end
